package com.vikutorika.service;

import cn.hutool.json.JSONUtil;
import com.vikutorika.dao.GroupTextMessageDAO;
import com.vikutorika.dao.GroupUserDAO;
import com.vikutorika.dao.PersonalTextMessageDAO;
import com.vikutorika.dao.imp.*;
import com.vikutorika.entity.*;
import com.vikutorika.vo.*;

import java.util.LinkedList;
import java.util.List;

public class MessageService {
    private MessageService() {
    }

    public static ResultVo newPersonalMessage(SendMessageVo messageVo) {
        PersonalTextMessageDAOImp dao = new PersonalTextMessageDAOImp();
        Integer insert = dao.insert(messageVo);
        if (insert == 1)
            return ResultVo.success("插入数据成功");
        return ResultVo.error("插入数据失败");
    }

    public static ResultVo newGroupMessage(SendMessageVo messageVo) {
        GroupTextMessageDAOImp dao = new GroupTextMessageDAOImp();
        int insert = dao.insert(messageVo);
        if (insert == 1)
            return ResultVo.success("插入数据成功");
        return ResultVo.error("插入数据失败");
    }

    public static ResultVo PullPersonalHistory(PullMessageRequestVo vo) {
        PersonalTextMessageDAO personalTextMessageDAO = new PersonalTextMessageDAOImp();
        List<PersonalTextMessage> all =
                personalTextMessageDAO.getBySenderUID_AND_ReceiverUID(vo.getId(), vo.getUid(), vo.getCount());
        List<MessageVo> list = new LinkedList<>();
        UserInfoDAOImp userInfoDAOImp = new UserInfoDAOImp();
        for (PersonalTextMessage p : all) {
            String src = userInfoDAOImp.getHeadPortraits(p.getSender_uid());
            list.add(new MessageVo(p.getMid(), "私聊", null, p.getSender_uid(), p.getReceiver_uid(), p.getCreat_time(), p.getContent(), p.getState(), src));
        }
        if (all != null && !all.isEmpty()) {
            personalTextMessageDAO.setRead(vo.getId(), vo.getUid(), all.get(0).getCreat_time());
        }
        return ResultVo.success(list);
    }

    public static ResultVo PullGroupHistory(PullMessageRequestVo vo) {
        GroupTextMessageDAO groupTextMessageDAO = new GroupTextMessageDAOImp();
        List<GroupTextMessage> messages = groupTextMessageDAO.getByGID_Recent(vo.getId(), vo.getCount());
        List<MessageVo> list = new LinkedList<>();
        UserInfoDAOImp userInfoDAOImp = new UserInfoDAOImp();
        for (GroupTextMessage p : messages) {
            String src = userInfoDAOImp.getHeadPortraits(p.getSender_uid());
            list.add(new MessageVo(p.getMid(), "群聊", p.getGid(), p.getSender_uid(), null, p.getCreate_time(), p.getContent(), null, src));
        }
        return ResultVo.success(list);
    }

    public static ResultVo PullPersonalUnread(PullMessageRequestVo vo, Integer uid) {
        PersonalTextMessageDAO personalTextMessageDAO = new PersonalTextMessageDAOImp();
        List<PersonalTextMessage> unRead =
                personalTextMessageDAO.getUnReadBySenderUID_AND_ReceiverUID(vo.getId(), uid);
        if (unRead != null && !unRead.isEmpty()) {
            personalTextMessageDAO.setRead(vo.getId(), uid, unRead.get(0).getCreat_time());
        }
        List<MessageVo> list = new LinkedList<>();
        UserInfoDAOImp userInfoDAOImp = new UserInfoDAOImp();
        for (PersonalTextMessage p : unRead) {
            String src = userInfoDAOImp.getHeadPortraits(p.getSender_uid());
            list.add(new MessageVo(p.getMid(), "私聊", null, p.getSender_uid(), p.getReceiver_uid(), p.getCreat_time(), p.getContent(), p.getState(), src));
        }
        return ResultVo.success(list);
    }
    //登陆后，根据UID获取该用户所有未读消息摘要
    public static ResultVo getAllUnreadDigest(Integer uid){
        List<UnreadMessageEventVo> ulist = new LinkedList<>();
        PersonalTextMessageDAO personalTextMessageDAO = new PersonalTextMessageDAOImp();
        FriendRelationDAOImp friendRelationDAOImp = new FriendRelationDAOImp();
        GroupUserDAO groupUserDAO = new GroupUserDAOImp();
        GroupTextMessageDAO groupTextMessageDAO = new GroupTextMessageDAOImp();
        List<GroupUser> groupUsers = groupUserDAO.getByUID(uid);
        for (GroupUser groupUser : groupUsers) {
            List<GroupTextMessage> gUnread = groupTextMessageDAO.getByGID_Unread(groupUser.getGid(),groupUser.getPull_time());
            if(!gUnread.isEmpty())
                ulist.add(new UnreadMessageEventVo("群聊",groupUser.getGid(), gUnread.size(),
                        gUnread.get(0).getContent()));
        }
        List<UserInfo> userInfos = friendRelationDAOImp.listMyFriends(uid);
        for (UserInfo userInfo : userInfos) {
            List<PersonalTextMessage> pUnread = personalTextMessageDAO.getUnReadBySenderUID_AND_ReceiverUID(userInfo.getUid(), uid);
            if(!pUnread.isEmpty())
                ulist.add(new UnreadMessageEventVo("私聊",userInfo.getUid(), pUnread.size(),
                        pUnread.get(0).getContent()));
        }
        return ResultVo.success("拉取离线未读消息摘要成功",ulist);
    }

    public static ResultVo PullGroupUnread(PullMessageRequestVo vo, Integer uid) {
        GroupTextMessageDAO dao = new GroupTextMessageDAOImp();
        GroupUserDAO groupUserDAO = new GroupUserDAOImp();
        GroupUser user = groupUserDAO.getByGID_AND_UID(vo.getId(), uid);
        List<GroupTextMessage> msgs = dao.getByGID_Unread(vo.getId(), user.getPull_time());
        if (!msgs.isEmpty()) {
            user.setPull_time(msgs.get(0).getCreate_time());
            groupUserDAO.update(user);
        }

        List<MessageVo> list = new LinkedList<>();
        UserInfoDAOImp userInfoDAOImp = new UserInfoDAOImp();
        for (GroupTextMessage p : msgs) {
            String src = userInfoDAOImp.getHeadPortraits(p.getSender_uid());
            list.add(new MessageVo(p.getMid(), "群聊", p.getGid(), p.getSender_uid(), null, p.getCreate_time(), p.getContent(), null, src));
        }
        return ResultVo.success(list);
    }
}
